//
//  BlurView.m
//  CeFuBaoDemo
//
//  Created by 关旭 on 2019/8/20.
//  Copyright © 2019 guanxu. All rights reserved.
//

#import "BlurView.h"

@implementation BlurView

- (instancetype)initWithFrame:(CGRect )frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.opaque = NO; // 设置为透明的
    }
    
    return self;
}
- (void)drawRect:(CGRect )rect {
    [[UIColor clearColor] setFill];//设置颜色为黑色
//    半透明区域
    UIRectFill(rect);//当前绘图环境所创建的内存中的图片上填充一个矩形
    
//    透明区域
//    CGRect clearRect = CGRectMake(60,80,self.frame.size.width-120,self.frame.size.height-220-80);
//
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    CGContextSetRGBStrokeColor(context, 1, 1, 1, 1);
//    CGContextAddEllipseInRect(context, clearRect);
//    CGContextDrawPath(context, kCGPathStroke);
    
    
//    视图相交区域
//    CGRect clearInstersection = CGRectIntersection(clearRect, rect);
////    相交区域设置为透明
//    [[UIColor clearColor] setFill];
////    把透明视图填充到图片上
//    UIRectFill(clearInstersection);
    
    
//    中间镂空部分
    CGRect clearRect = CGRectMake(60,80,self.frame.size.width-120,self.frame.size.height-220-80);
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:0];
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:clearRect];
    [path appendPath:circlePath];
    [path setUsesEvenOddFillRule:YES];
    
    CAShapeLayer *fillLayer = [CAShapeLayer layer];
    fillLayer.path = path.CGPath;
    fillLayer.fillRule = kCAFillRuleEvenOdd;//中间镂空的关键点 填充规则
    fillLayer.fillColor = [UIColor blackColor].CGColor;
    fillLayer.opacity = 0.5;
    
    fillLayer.strokeColor = [UIColor whiteColor].CGColor;
    fillLayer.shadowColor = [UIColor blackColor].CGColor;
    fillLayer.shadowOpacity = 0.9;
    fillLayer.shadowOffset = CGSizeMake(0, 0);
    fillLayer.shadowRadius = 10.0;

    [self.layer addSublayer:fillLayer];

}
@end
